<template>
	<view>
		<navbar :autoBack='true'></navbar>
		<view class="con u-flex u-flex-column u-flex-items-center">
			<view class="logo">

			</view>
			<view class="title">
				欢迎来到贷款平台
			</view>
			<view class="form">
				<view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" type="text" v-model="form.username" placeholder="请输入姓名" />
				</view>
				<view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" type="number" maxlength="11" v-model="form.mobile" placeholder="请输入电话号码" />
				</view>
				<view class="inpbox">
					<image src="/static/login/yzm.png" class="inpbox__yzm" mode=""></image>
					<input class="inpbox__inp" type="number" v-model="form.code" maxlength="6" placeholder="请输入验证码" />
					<u-button color="#3262FD" @tap="getCode"
						:customStyle='{width:"200rpx",height:"84rpx",borderRadius:"15rpx",marginRight:"10rpx",fontSize:"24rpx"}'>{{tips}}</u-button>
				</view>
				<view class="inpbox">
					<image src="/static/login/pws.png" class="inpbox__pws" mode=""></image>
					<input class="inpbox__inp" type="password" placeholder="请输入密码" v-model="form.password" />
				</view>
				<view class="inpbox">
					<image src="/static/login/yqm.png" class="inpbox__yqm" mode=""></image>
					<input class="inpbox__inp" type="text" placeholder="请输入推荐码" v-model="form.invate" />
				</view>
			</view>
			<u-button text="注册" color="#3262FD" size="large" @click="submit" :loading="loading"
				:customStyle='{width:"606rpx",height:"111rpx",borderRadius:"15rpx",marginTop:"30rpx"}'></u-button>
		</view>
		<u-code change-text='Xs重新获取' :seconds="seconds" @end="end" @start="start" ref="uCode"
			@change="codeChange"></u-code>
	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {
		
		data() {
			return {
				loading:false,
				tips: '',
				// refCode: null,
				seconds: 60,
				form: {
					"username": "",
					"mobile": "",
					"password": "",
					"code": "",
					"invate": ""
				}
			};
		},
		onReady() {
			// 注意这里不能将一个组件赋值给data的一个变量，否则在微信小程序会
			// 造成循环引用而报错，如果你想这样做，请在非data中定义refCode变量
			this.refCode = this.$refs.uCode;
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			submit(){
				let {username,mobile,password,code,invate} = this.form
				if(!username){
					uni.$u.toast('请输入姓名')
					return
				}
				if(!mobile){
					uni.$u.toast('请输入手机号')
					return
				}
				if(!code){
					uni.$u.toast('请输入验证码')
					return
				}
				if(!password){
					uni.$u.toast('请输入密码')
					return
				}
				if(!invate){
					uni.$u.toast('请输入邀请码')
					return
				}
				let _this=this
				this.loading=true
				user.register(this.form).then(res=>{
					if(res.data.code==1){
						uni.$u.toast(res.data.msg)
						uni.setStorageSync('dktoken',res.data.data.userinfo.token)
						uni.setStorageSync('dkuserinfo',res.data.data.userinfo)
						setTimeout(()=>{
							uni.redirectTo({
								url:'/pages/index/index'
							})
							_this.loading=false
						},1000)
					}else{
						_this.loading=false
					}
				})
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			end() {
				uni.$u.toast('倒计时结束');
			},
			start() {
				uni.$u.toast('倒计时开始');
			}
		}
	}
</script>

<style lang="scss">
	.con {
		padding-top: 50rpx;
		position: relative;
		padding-bottom: 120rpx;
	}

	.logo {
		width: 224rpx;
		height: 225rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 7rpx 40rpx 0rpx rgba(72, 139, 254, 0.18);
		border-radius: 90rpx;
	}

	.title {
		width: 326rpx;
		height: 39rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 41rpx;
		color: #1E1E1E;
		line-height: 41rpx;
		margin: 70rpx 0 79rpx;
	}

	.inpbox {
		width: 606rpx;
		height: 107rpx;
		background: #F6F9FF;
		border-radius: 15rpx;
		display: flex;
		align-items: center;
		margin-bottom: 33rpx;

		&__pws {
			width: 32rpx;
			height: 37rpx;
			margin-left: 46rpx;
			margin-right: 28rpx;
		}

		&__phone {
			width: 28rpx;
			height: 35rpx;
			margin: 0 30rpx 0 46rpx;
		}

		&__yzm {
			width: 28rpx;
			height: 33rpx;
			margin: 0 30rpx 0 48rpx;
		}

		&__yqm {
			width: 32rpx;
			height: 33rpx;
			margin: 0 28rpx 0 48rpx;
		}

		&__inp {
			flex: 1;
			font-family: PingFang SC;
			font-size: 28rpx;
			color: #333;
		}

		.uni-input-placeholder {
			color: #A1A4AA;
			font-family: PingFang SC;
			font-size: 28rpx;
		}
	}

	.tips {
		width: 606rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 26rpx;
		color: #A9ADB8;
		line-height: 26rpx;
		margin-top: 34rpx;
		margin-bottom: 100rpx;
	}

	.tips1 {
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #A1A4AA;
		line-height: 28rpx;
	}

	.wx {
		width: 112rpx;
		height: 112rpx;
		margin-top: 29rpx;
	}

	.u-button--normal {
		padding: 0;
	}
</style>